#!/usr/bin/env python
#    This file is part of django_ip2country.
#    django_ip2country is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    django_ip2country is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with django_ip2country.  If not, see <http://www.gnu.org/licenses/>.
#    Author: Esteban Feldman esteban.feldman@gmail.com


import urllib
from zipfile import ZipFile


TARGET = 'http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip'
OUT_FILE_NAME = 'ip-to-country.zip'

def reporthook(blocknum, bs, size):
	elapsed = (blocknum * bs) * 100 / size
	if elapsed > 100: elapsed = 100
	print '%s%%' % elapsed

def main():
	print 'Downloading ip2country database file'
	urllib.urlretrieve(TARGET, OUT_FILE_NAME, reporthook=reporthook)
	
	print 'Extracting data file'
	zf = ZipFile(OUT_FILE_NAME)
	zf.extractall()

if __name__ == '__main__':
	main()